package com.central.apps.common.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
;

/**
 * Created by Ocean lin on 2019/5/15.
 *
 * @author Ocean lin
 */
public class DateUtiles {
    public static final String DATE_PATTERN = "yyyy-MM-dd";
    public static final String DATEPATTERN = "yyyyMMdd";
    public static final String DATE_PATTERN_24 = "yyyy-MM-dd HH:mm:ss";


    public static String getYmd(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(DateUtiles.DATE_PATTERN);
        String format = sdf.format(date);
        return format;
    }
    public static String getYmdNew(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(DateUtiles.DATEPATTERN);
        String format = sdf.format(date);
        return format;
    }
    public static String getYmdhms(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(DateUtiles.DATE_PATTERN_24);
        String format = sdf.format(date);
        return format;
    }

    /**
     * 比较时间是否相等
     *
     * @param date1 时间1
     * @param date2 时间2
     * @return
     */
    public static boolean isSameDay(Date date1, Date date2) {
        if (date1 != null && date2 != null) {
            Calendar cal1 = Calendar.getInstance();
            cal1.setTime(date1);
            Calendar cal2 = Calendar.getInstance();
            cal2.setTime(date2);
            return isSameDay(cal1, cal2);
        } else {
            throw new IllegalArgumentException("The date must not be null");
        }
    }

    /**
     * 比较时间是否相等
     *
     * @param cal1
     * @param cal2
     * @return
     */
    public static boolean isSameDay(Calendar cal1, Calendar cal2) {
        if (cal1 != null && cal2 != null) {
            return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH) && cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH) &&
                    cal1.get(Calendar.HOUR_OF_DAY) == cal2.get(Calendar.HOUR_OF_DAY) && cal1.get(Calendar.MINUTE) == cal2.get(Calendar.MINUTE);
        } else {
            throw new IllegalArgumentException("The date must not be null");
        }
    }


    /**
     * 判断时间是否在起止时间范围内
     *
     * @param nowTime   传入时间
     * @param beginTime 起始时间
     * @param endTime   终止时间
     * @return
     */
    public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * 日期相加减天数
     *
     * @param date        如果为Null，则为当前时间
     * @param days        加减天数
     * @param includeTime 是否包括时分秒,true表示包含
     * @return
     * @throws ParseException
     */
    public static Date dateAdd(Date date, int days, boolean includeTime) throws ParseException {
        if (date == null) {
            date = new Date();
        }
        if (!includeTime) {
            SimpleDateFormat sdf = new SimpleDateFormat(DateUtiles.DATE_PATTERN);
            date = sdf.parse(sdf.format(date));
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, days);
        return cal.getTime();
    }


    /**
     * @param date
     * @param hour
     * @param includeTime
     * @return
     * @throws ParseException
     */
    public static Date hourAdd(Date date, int hour, boolean includeTime) {
        if (date == null) {
            date = new Date();
        }
        if (!includeTime) {
            SimpleDateFormat sdf = new SimpleDateFormat(DateUtiles.DATE_PATTERN_24);
            try {
                date = sdf.parse(sdf.format(date));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR, hour);
        return cal.getTime();
    }


    public static Date parseStrToDate(String time) {
        if (null == time || "".equals(time)) {
            return null;
        }

        Date date = null;
        try {
            DateFormat dateFormat = new SimpleDateFormat(DATE_PATTERN_24);
            date = dateFormat.parse(time);
        } catch (Exception e) {

        }
        return date;
    }

    public static int calIntervalMonth(Date fromDate, Date toDate) throws ParseException {
        int result = 0;

        if (fromDate.after(toDate)) {
            return 0;
        }

        Calendar cFrom = Calendar.getInstance();
        Calendar cTo = Calendar.getInstance();
        cFrom.setTime(fromDate);
        cTo.setTime(toDate);

        int cFromYear = cFrom.get(Calendar.YEAR);
        int cToYear = cTo.get(Calendar.YEAR);
        int cFromMonth = cFrom.get(Calendar.MONTH);
        int cToMonth = cTo.get(Calendar.MONTH);
        int cFromDay = cFrom.get(Calendar.DAY_OF_MONTH);
        int cToDay = cTo.get(Calendar.DAY_OF_MONTH);

        if (cToDay < cFromDay) {
            cToDay = cToDay + calPerMonthDay(cToYear, cToMonth - 1);
            cToMonth = cToMonth - 1;
            if (cToMonth == 0) {
                cToMonth = 12;
                cToYear = cToYear - 1;
            }
        }

        if (cFromMonth > cToMonth) {
            cToYear = cToYear - 1;
            cToMonth = cToMonth + 12;
        }

        result = (cToYear - cFromYear) * 12 + (cToMonth - cFromMonth) + (cToDay - cFromDay) > 15 ? 1 : 0;
        return result == 0 ? 1 : Math.abs(result);
    }

    private static int calPerMonthDay(int year, int month) {
        switch (month) {
            case 4:
            case 6:
            case 9:
            case 11:
                return 30;
            case 2:
                if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
                    return 29;
                } else {
                    return 28;
                }
            default: return 31;
        }
    }
}
